package com.hetao.assist.service.mq;


import com.hetao.assist.service.util.Common;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.impl.DefaultExceptionHandler;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.amqp.RabbitProperties;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMqConfig {

    public static String FANOUT_SMS_QUEUE="fanout_sms_queue";

    /**
     * 创建队列
     * @return
     */
    @Bean
    public Queue fanoutSmsQueue() {
        return new Queue(FANOUT_SMS_QUEUE + Common.getLocalIp());
    }

    /**
     * 创建交换机
     * @return
     */
    @Bean
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange("fanoutExchange1");
    }

    /**
     * 队列与交换机进行绑定
     */
    @Bean
    Binding bindingSms() {
        return BindingBuilder.bind(fanoutSmsQueue()).to(fanoutExchange());
    }

    @Bean
    public CachingConnectionFactory mainMqconnectionFactory(RabbitProperties rabbitProperties) {
        ConnectionFactory rabbitfactory = new ConnectionFactory();
        rabbitfactory.setHost(rabbitProperties.getHost());
        rabbitfactory.setPort(rabbitProperties.getPort());
        rabbitfactory.setUsername(rabbitProperties.getUsername());
        rabbitfactory.setPassword(rabbitProperties.getPassword());
        rabbitfactory.setExceptionHandler(new DefaultExceptionHandler());
        rabbitfactory.setVirtualHost(rabbitProperties.getVirtualHost());
//		rabbitfactory.setRequestedHeartbeat(config.getRequestedHeartbeat());
//		rabbitfactory.setConnectionTimeout(config.getConnectionTimeout());
        //spring自己有套机制已经做好了
        rabbitfactory.setAutomaticRecoveryEnabled(false);

        return new CachingConnectionFactory(rabbitfactory);
    }


    @Bean
    public SimpleRabbitListenerContainerFactory myFactory(
            SimpleRabbitListenerContainerFactoryConfigurer configurer,
            @Qualifier("mainMqconnectionFactory") CachingConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory =
                new SimpleRabbitListenerContainerFactory();
        configurer.configure(factory, connectionFactory);
        factory.setMessageConverter(new Jackson2JsonMessageConverter());
        return factory;
    }

    @Bean
    public RabbitTemplate sendRabbitTemplate(
            @Qualifier("mainMqconnectionFactory")CachingConnectionFactory connectionFactory) {
        RabbitTemplate template = new RabbitTemplate(connectionFactory);
        template.setMessageConverter(new Jackson2JsonMessageConverter());
        return template;
    }

}

